package com.cjhedu.web.service.impl;

import com.cjhedu.web.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author 陈佳豪
 * @version 1.0
 */
@Service
public class FileServiceImpl implements FileService {

    //本地储存文件
    @Override
    public String saveFile(MultipartFile file) throws IOException {
        //1.获取真实文件名(获取文件后最)
        String originalFilename = file.getOriginalFilename();
        //String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //String[] arr = originalFilename.split(".");
        //String ext = arr[arr.length - 1];
        //2.问了避免文件名重复,随机生成一个不重复的文件名
        String name = UUID.randomUUID().toString() + "."+substring;
        File file1 = new File("D://a//" + name);
        //3.真正保存
        file.transferTo(file1);
        return file1.getAbsolutePath();

    }
}
